#pragma once
#include "XCom.h"
#include "protobuf/xmsg_type.pb.h"
//头部消息的最大字节数
#define MAX_MSG_SIZE 8192

#define API_GATEWAY_PORT 20010
#define API_GATEWAY_SSL_PORT 20011
#define API_GATEWAY_NAME "gw"
#define REGISTER_PORT 20018
#define CONFIG_NAME "config"
#define CONFIG_PORT 20019
//所有的函数做内联
class XCOM_API XMsg
{
public:
    //数据大小
    int size = 0;

    //消息类型,0表示没有消息
    unsigned int type = 0;

    //数据存放（protobuf的序列化后的数据）
    char *data = 0;

    //已经接收的数据大小
    int recv_size = 0;


    bool Alloc(int s)
    {
        if (s <= 0 || s > MAX_MSG_SIZE)
            return false;
        if (data)
            delete data;
        data = new char[s];
        if (!data)
            return false;
        this->size = s;
        this->recv_size = 0;
        return true;
    }

    //判断数据是否接收完成
    bool Recved()
    {
        if (size <= 0)return false;
        return (recv_size == size);
    }

    void Clear()
    {
        delete data;
        memset(this, 0, sizeof(XMsg));
    }

};

